﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Lexalytics;

namespace SalienceEngineDemo.Dialogs
{
    public partial class EntityDetails : Form
    {
        private List<SalienceEntity> m_Entities = null;
        private int m_EntityIndex = 0;
        private string m_Content = String.Empty;
        private DataGridView m_SourceView = null;

        public EntityDetails(DataGridView dgSource, List<SalienceEntity> lstEntities, string strContent)
        {
            m_EntityIndex = Convert.ToInt32(dgSource.SelectedRows[0].Tag);
            m_Entities = lstEntities;
            m_Content = strContent;
            m_SourceView = dgSource;
            InitializeComponent();
        }

        #region Event Handlers
        private void EntityDetails_Load(object sender, EventArgs e)
        {
            PopulateFormData();
            SetNavButtons();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (m_EntityIndex > 0)
            {
                m_SourceView.Rows[m_EntityIndex].Selected = false;
                m_EntityIndex--;
                m_SourceView.Rows[m_EntityIndex].Selected = true;
                PopulateFormData();
                SetNavButtons();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (m_EntityIndex < m_Entities.Count - 1)
            {
                m_SourceView.Rows[m_EntityIndex].Selected = false;
                m_EntityIndex++;
                m_SourceView.Rows[m_EntityIndex].Selected = true;
                PopulateFormData();
                SetNavButtons();
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion // Event Handlers

        private void SetNavButtons()
        {
            btnPrev.Enabled = (m_EntityIndex > 0);
            btnNext.Enabled = (m_EntityIndex < m_Entities.Count - 1);
        }

        private void PopulateFormData()
        {
            Lexalytics.SalienceEntity anEntity = m_Entities[m_EntityIndex];

            // Set dialog title
            this.Text = String.Format("Details for {0}", anEntity.sNormalizedForm);
            // Insert general entity details
            lblNormalizedForm.Text = anEntity.sNormalizedForm;
            if (anEntity.sLabel.CompareTo(anEntity.sType) == 0)
                lblType.Text = String.Format("Entity type: {0}", anEntity.sType);
            else
                lblType.Text = String.Format("Entity type: {0} ({1})", anEntity.sType, anEntity.sLabel);

            switch (anEntity.sType)
            {
                case "Company":
                    EntityTypeImage.Image = lstTypeImages.Images[0];
                    break;
                case "Person":
                    EntityTypeImage.Image = lstTypeImages.Images[1];
                    break;
                case "Place":
                    EntityTypeImage.Image = lstTypeImages.Images[2];
                    break;
                case "Product":
                    EntityTypeImage.Image = lstTypeImages.Images[3];
                    break;
                default:
                    EntityTypeImage.Image = lstTypeImages.Images[4];
                    break;
            }
            lblSentiment.Text = String.Format("Sentiment score: {0}", anEntity.fSentimentScore);
            lblEvidence.Text = String.Format("Evidence score: {0}", anEntity.nEvidence);
            txtSummary.Text = anEntity.sSummary;

            // Insert mention list
            int nMentionCount = 0;
            dgMentions.Rows.Clear();
            foreach (SalienceMention aMention in anEntity.Mentions)
            {
                dgMentions.Rows.Add();
                switch (aMention.nType)
                {
                    case 0:
                        dgMentions.Rows[nMentionCount].Cells[0].Value = "Named";
                        break;
                    case 1:
                        dgMentions.Rows[nMentionCount].Cells[0].Value = "Nominal";
                        break;
                    case 2:
                        dgMentions.Rows[nMentionCount].Cells[0].Value = "Pronominal";
                        break;
                    default:
                        dgMentions.Rows[nMentionCount].Cells[0].Value = aMention.nType.ToString();
                        break;
                }
                dgMentions.Rows[nMentionCount].Cells[1].Value = aMention.fScore;
                dgMentions.Rows[nMentionCount].Cells[2].Value = aMention.Phrase.sText;
                nMentionCount++;
            }

            // Insert theme list
            int nThemeCount = 0;
            dgThemes.Rows.Clear();
            foreach (SalienceTheme aTheme in anEntity.Themes)
            {
                dgThemes.Rows.Add();
                dgThemes.Rows[nThemeCount].Cells[0].Value = aTheme.sTheme;
                dgThemes.Rows[nThemeCount].Cells[1].Value = aTheme.nThemeType;
                dgThemes.Rows[nThemeCount].Cells[2].Value = aTheme.fScore;
                dgThemes.Rows[nThemeCount].Cells[3].Value = aTheme.nEvidence;
                nThemeCount++;
            }

            // Insert topic list
            int nTopicCount = 0;
            dgTopics.Rows.Clear();
            foreach (SalienceTopic aTopic in anEntity.Topics)
            {
                dgTopics.Rows.Add();
                dgTopics.Rows[nTopicCount].Cells[0].Value = aTopic.sTopic;
                dgTopics.Rows[nTopicCount].Cells[1].Value = aTopic.fSentiment;
                dgTopics.Rows[nTopicCount].Cells[2].Value = aTopic.fScore;
                dgTopics.Rows[nTopicCount].Cells[3].Value = aTopic.nHits;
                nTopicCount++;
            }

            // Insert sentiment phrases
            int nPhraseCount = 0;
            dgPhrases.Rows.Clear();
            foreach (SalienceSentimentPhrase aPhrase in anEntity.SentimentPhrases)
            {
                dgPhrases.Rows.Add();
                dgPhrases.Rows[nPhraseCount].Cells[0].Value = aPhrase.Phrase.sText;
                dgPhrases.Rows[nPhraseCount].Cells[1].Value = aPhrase.fScore;
                if (aPhrase.nModified == 1)
                    dgPhrases.Rows[nPhraseCount].Cells[2].Value = "Yes";
                else
                    dgPhrases.Rows[nPhraseCount].Cells[2].Value = "No";
                dgPhrases.Rows[nPhraseCount].Cells[3].Value = aPhrase.sSource;
                nPhraseCount++;
            }

        }
    }
}
